﻿/**
 * Created by Maibenben on 2017/5/8.
 */
$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    $(".hot_search").hide();
    //进入页面读取之前的搜索记录
    var hislog = localStorage.getItem("history");
    if (hislog) {
        hislog = [...new Set(hislog.split(","))];
        hislog.forEach(function (v) {
            var hisList = document.createElement("li");
            hisList.innerHTML = v;
            hisList.className = "his_doc";
            $('.history_list').append(hisList);
        })
    } else {
        $(".history_search").remove();
    }
    $(".his_doc").on("click", function () {
        $("#search").val($(this).text());
    });
    var his = hislog || "";
    $("#search_btn").on("click", function () {
        //输入关键字后搜索并显示搜索的结果
        var keywords = $("#search").val();
        if (keywords == "") {
            $(".hot_list").html("");
            creatLayout("请输入要查询的医师");
            return false;
        } else {
            if (his) {
                his = his + "," + keywords;
            } else {
                his = his + keywords;
            }
            localStorage.setItem("history", his);
            $(".load").show();
            $(".hot_list").html("");
            $.get(searchURL + "?keywords=" + keywords + "&mid=" + $.cookie("mid") + "&token=" + $.cookie("token"), function (res) {
                $(".load").hide();
                $(".hot_search").show();
                if (res.data.total > 0) {
                    var listHTML = template("searchTem", res.data);
                    $(".hot_list").html(listHTML);
                    //点击相应的医生后跳转到指定的医生详情
                    $(".doctor_name").on("click", function () {
                        //根据id跳转医生详情页
                        window.location.href = "doctor_info.html?d_id=" + $(this).attr("data-id");
                    });
                } else {
                    creatLayout("未找到搜索的医师");
                    $("#search").val("");
                }
            });
        }
    });
    $('.del').on('click', function () {
        //并且删除本地的搜索缓存
        localStorage.removeItem("history");
        location.reload();
    })
});